﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Threading;

namespace WindowControlLib
{
    public class MerchandiseSearch
    {
        private IntPtr HwndMain;
        private IntPtr GetMainWindow()
        {

            IntPtr ViewHwnd = HasViewWindow();

            if (ViewHwnd.ToInt32() != 0)
            {
                this.LeaveViewWindow(ViewHwnd);

                Thread.Sleep(200);
            }

            return HasInputWindow();
        }

        private IList<IntPtr> pChildHandleList;
        private void LeaveViewWindow(IntPtr hWnd)
        {
            pChildHandleList = new List<IntPtr>();
            var enumChildProc = new WindowsAPILib.EnumChildProc(GetChildByClassName);
            WindowsAPI.EnumChildWindows(hWnd, enumChildProc, "ThunderRT6CommandButton");

            foreach (var intPtr in pChildHandleList)
            {
                if (WindowsAPILib.GetWindowText(intPtr).Contains("終了"))
                {
                    WindowsAPILib.ClickByPost(intPtr);
                }
            }
        }

        private bool GetChildByClassName(IntPtr pHWnd, string lParam)
        {

            if (WindowsAPILib.GetClassName(pHWnd, 100).Contains(lParam))
            {
                pChildHandleList.Add(pHWnd);
            }
            return true;//continue
        }

        private IntPtr HasInputWindow()//申込番号入力
        {
            IntPtr HWnd =
                WindowsAPI.FindWindow("ThunderRT6FormDC", null);
            string resStr =
            WindowsAPILib.GetWindowText(HWnd);

            if (resStr.Contains("申込番号入力"))
                return HWnd;
            else
            {
                throw new Exception(resStr + "\n" + resStr.Contains("申込番号入力"));
                return IntPtr.Zero;
            }
            
        }

        private IntPtr HasViewWindow()//申込番号入力
        {
            IntPtr HWnd =
                WindowsAPI.FindWindow("ThunderRT6FormDC", null);
            string resStr =
            WindowsAPILib.GetWindowText(HWnd);

            if (resStr.Contains("商品在庫"))
                return HWnd;
            else
                return IntPtr.Zero;
        }

        public bool Search(string MerchandiseNo)
        {
            this.HwndMain =
                   GetMainWindow();

            if (HwndMain == IntPtr.Zero)
            {
                return false;
            }

            IntPtr HwndText =
                this.GetTextControl();

            if (HwndText == IntPtr.Zero)
            {
                return false;
            }

            this.SetTextCtrlValue(HwndText, MerchandiseNo);

            this.SearchSubmit();

            int count = 0;
            int TimeOut = 1000 * 15;
            IntPtr ViewHwnd =
                HasViewWindow();
            while ((HasViewWindow() == IntPtr.Zero 
                    || !WindowsAPI.IsWindowVisible(ViewHwnd))
                    && count < TimeOut / 500)
            {
                Thread.Sleep(500);

                count++;
                ViewHwnd =
                HasViewWindow();
            }
            if (count == TimeOut / 500)
            {
                new Exception("TimeOut!");
            }

            return true;
        }

        private IntPtr GetTextControl()
        {
            pChildHandleList = new List<IntPtr>();

            var enumChildProc = new WindowsAPILib.EnumChildProc(GetChildByClassName);

            WindowsAPI.EnumChildWindows(HwndMain, enumChildProc, "TextEditX");

            foreach (var intPtr in pChildHandleList)
            {
                if (this.GetTextCtrlSTPostion(intPtr)[0] == 248)//248,72 is first position Y add by 20 in 0-4
                    return intPtr;
            }

            return IntPtr.Zero;
        }

        private int[] GetTextCtrlSTPostion(IntPtr HWndText)
        {
            int[] res = new int[2];
            Rectangle rect;
            Rectangle rectMain;
            WindowsAPI.GetWindowRect(HwndMain, out rectMain);
            WindowsAPI.GetWindowRect(HWndText, out rect);

            res[0] = rect.X - rectMain.X;
            res[1] = rect.X - rectMain.X;

            return res;
        }

        private void SetTextCtrlValue(IntPtr pHwndText, string pMerchandiseNo)
        {
            this.ActiveTextControl(pHwndText);
            this.ActiveTextControl(pHwndText);//2012.12.03 为清理残值

            WindowsAPILib.SetTextBySend(pHwndText, pMerchandiseNo);

            WindowsAPILib.TabPost(HwndMain);
            Thread.Sleep(50);


        }

        private void ActiveTextControl(IntPtr pHwndText)
        {

            while (WindowsAPI.IsWindowVisible(pHwndText))
            {
                WindowsAPILib.SetTextBySend(pHwndText, "");//2012.12.03 if 查询画面已经有多值，会查第二个位置上的番号，所以清理
                WindowsAPILib.TabPost(HwndMain);
                Thread.Sleep(50);
            }
            while (!WindowsAPI.IsWindowVisible(pHwndText))
            {
                WindowsAPILib.TabPost(HwndMain);
                Thread.Sleep(50);
            }
        }

        private bool SearchSubmit()
        {
            WindowsAPI.SetForegroundWindow(HwndMain);

            pChildHandleList = new List<IntPtr>();
            var enumChildProc = new WindowsAPILib.EnumChildProc(GetChildByClassName);
            WindowsAPI.EnumChildWindows(HwndMain, enumChildProc, "ThunderRT6CommandButton");

            foreach (var intPtr in pChildHandleList)
            {
                if (WindowsAPILib.GetWindowText(intPtr).Contains("商品在庫"))
                {
                    WindowsAPILib.ClickByPost(intPtr);
                    return true;
                }
            }
            
            return false;
        }


    }
}

